
#include "debug.h"
#include "memmap.h"
#include "storage.h"
#include "Config.h"

config_t AA_Config;

#define   OUTPUT_TIMEOUT_MAX_HOUR		4		

#define   OUTPUT_TIMEOUT_MAX 	(OUTPUT_TIMEOUT_MAX_HOUR*3600) /* in sec */

//#define   RENEW_CONFIG	1

void AA_Config_Init ( void )
{

#ifdef   RENEW_CONFIG
	AA_Config.time_unit = AA_CONFIG_TIME_UNIT_SEC; 
	AA_Config.output_timeout = AA_CONFIG_TIMEOUT_DEFAULT; 
	AA_Config.output_timeout_max = OUTPUT_TIMEOUT_MAX; 
	storage.Write(ADDR_AA_CONFIG, (unsigned char*)&AA_Config, sizeof(AA_Config));
	storage.Read(ADDR_AA_CONFIG, (unsigned char*)&AA_Config, sizeof(AA_Config));
#else
	storage.Read(ADDR_AA_CONFIG, (unsigned char*)&AA_Config, sizeof(AA_Config));

	if ( AA_Config.time_unit > AA_CONFIG_TIME_UNIT_HOUR )
	{ 
		AA_Config.time_unit = AA_CONFIG_TIME_UNIT_SEC; 
		AA_Config.output_timeout = AA_CONFIG_TIMEOUT_DEFAULT; 
		
		storage.Write(ADDR_AA_CONFIG, (unsigned char*)&AA_Config, sizeof(AA_Config));
		storage.Read(ADDR_AA_CONFIG, (unsigned char*)&AA_Config, sizeof(AA_Config));
	}
#endif

	DBG("\n%s; AA_Config.time_unit:          %d\n",__FUNCTION__,AA_Config.time_unit); 
	DBG("\n%s; AA_Config.output_timeout:     %d\n",__FUNCTION__,AA_Config.output_timeout); 
	DBG("\n%s; AA_Config.output_timeout_max: %d\n",__FUNCTION__,AA_Config.output_timeout_max); 
}


__inline__ void AA_Config_Save ( void )
{
	storage.Write(ADDR_AA_CONFIG, (unsigned char*)&AA_Config, sizeof(AA_Config));
}
